package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.business.domain.OperationRemind;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.service.IOperationRemindService;
import com.ruoyi.business.service.IWxUserService;
import com.ruoyi.business.vo.OperationRemindType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.RentalHouseMapper;
import com.ruoyi.business.domain.RentalHouse;
import com.ruoyi.business.service.IRentalHouseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出租房源Service业务层处理
 * 
 * @author asher
 * @date 2021-12-02
 */
@Service
public class RentalHouseServiceImpl implements IRentalHouseService 
{
    @Autowired
    private RentalHouseMapper rentalHouseMapper;

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private IOperationRemindService operationRemindService;

    /**
     * 查询出租房源
     * 
     * @param rentalHouseId 出租房源主键
     * @return 出租房源
     */
    @Override
    public RentalHouse selectRentalHouseByRentalHouseId(Long rentalHouseId)
    {
        return rentalHouseMapper.selectRentalHouseByRentalHouseId(rentalHouseId);
    }

    /**
     * 查询出租房源列表
     * 
     * @param rentalHouse 出租房源
     * @return 出租房源
     */
    @Override
    public List<RentalHouse> selectRentalHouseList(RentalHouse rentalHouse)
    {
        return rentalHouseMapper.selectRentalHouseList(rentalHouse);
    }

    /**
     * 新增出租房源
     * 
     * @param rentalHouse 出租房源
     * @return 结果
     */
    @Override
    @Transactional
    public int insertRentalHouse(RentalHouse rentalHouse)
    {
        rentalHouse.setCreateTime(DateUtils.getNowDate());
        int i = rentalHouseMapper.insertRentalHouse(rentalHouse);
        List<WxUser> wxUsers = wxUserService.selectWxUserList(new WxUser());
        if (wxUsers.isEmpty()) {
            return i;
        }
        List<OperationRemind> list = new ArrayList<>();
        for (WxUser item : wxUsers) {
            list.add(new OperationRemind(OperationRemindType.RENT_HOUSE, "[房屋出租]您有一条新的房屋出租信息尚未读取!", rentalHouse.getRentalHouseId(), "0", item.getUserId(), null, DateUtils.getNowDate(), rentalHouse.getCreateBy()));
        }
        operationRemindService.batchOperationRemind(list);
        return i;
    }

    /**
     * 修改出租房源
     * 
     * @param rentalHouse 出租房源
     * @return 结果
     */
    @Override
    public int updateRentalHouse(RentalHouse rentalHouse)
    {
        rentalHouse.setUpdateTime(DateUtils.getNowDate());
        return rentalHouseMapper.updateRentalHouse(rentalHouse);
    }

    /**
     * 批量删除出租房源
     * 
     * @param rentalHouseIds 需要删除的出租房源主键
     * @return 结果
     */
    @Override
    public int deleteRentalHouseByRentalHouseIds(Long[] rentalHouseIds)
    {
        ArrayList<Long> integers = new ArrayList<>(Arrays.asList(rentalHouseIds));
        for (Long item : integers) {
            operationRemindService.deleteOperationRemindByBusinessId(Long.valueOf(item));
        }
        return rentalHouseMapper.deleteRentalHouseByRentalHouseIds(rentalHouseIds);
    }

    /**
     * 删除出租房源信息
     * 
     * @param rentalHouseId 出租房源主键
     * @return 结果
     */
    @Override
    public int deleteRentalHouseByRentalHouseId(Long rentalHouseId)
    {
        operationRemindService.deleteOperationRemindByBusinessId(rentalHouseId);
        return rentalHouseMapper.deleteRentalHouseByRentalHouseId(rentalHouseId);
    }
}
